/*
On startup, connect to the "ping_pong" app.
*/
var port = browser.runtime.connectNative("ping_pong");
var portFromCS;
/*
Listen for messages from the app.
*/
port.onMessage.addListener((response) => {
  console.log("Received: " + response);
  if(portFromCS!=undefined){
    console.log("portFromCS will post.");
    portFromCS.postMessage(response);
  }
});

/*
On a click on the browser action, send the app a message.
*/
browser.browserAction.onClicked.addListener(() => {
  console.log("Sending:  ping");
  port.postMessage("ping");
});

// from cs 


function connected(p) {
  portFromCS = p;
  portFromCS.onMessage.addListener(function(m) {
    console.log("In background script, received message from content script");
    port.postMessage(m);
    console.log("send to native.");
  });
}

browser.runtime.onConnect.addListener(connected);
